﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChurchData.Business;

namespace ChurchData
{
    public partial class PeopleAddressUC : UserControl
    {
        #region Fields / Properties
        public bool HasUnsavedChanges { get; set; }

        #endregion Fields / Properties

        #region Constructor
        public PeopleAddressUC()
        {
            InitializeComponent();

            SetControlEvents();
        }

        #endregion Constructor

        #region Control Methods
        private void SetControlEvents()
        {
            txtLine1.TextChanged += new EventHandler(ControlValueChange);
            txtLine2.TextChanged += new EventHandler(ControlValueChange);
            txtCity.TextChanged += new EventHandler(ControlValueChange);
            txtState.TextChanged += new EventHandler(ControlValueChange);
            txtZip.TextChanged += new EventHandler(ControlValueChange);
            txtFromDate.TextChanged += new EventHandler(ControlValueChange);
            txtToDate.TextChanged += new EventHandler(ControlValueChange);
            txtNotes.TextChanged += new EventHandler(ControlValueChange);
        }

        private void ControlValueChange(object sender, EventArgs e)
        {
            HasUnsavedChanges = true;
        }

        #endregion Control Methods

        #region Populate & Save Methods
        public void LoadAddress(int addressPK)
        {
            AddressBO aBO = new AddressBO();
            Address drA = aBO.GetRecordByPK(addressPK);
            this.txtLine1.Text = drA.Line1;
            this.txtLine2.Text = drA.Line2;
            this.txtCity.Text = drA.City;
            this.txtState.Text = drA.StateRegion;
            this.txtZip.Text = drA.Zipcode;
            this.txtNotes.Text = drA.Notes;
            this.txtFromDate.Text = drA.FromDate;
            this.txtToDate.Text = drA.ToDate;
            this.HasUnsavedChanges = false;
        }

        public int SaveAddress(int addressPK)
        {
            Address drA = new Address();
            drA.Line1 = txtLine1.Text.Trim();
            drA.Line2 = txtLine2.Text.Trim();
            drA.City = txtCity.Text.Trim();
            drA.StateRegion = txtState.Text.Trim();
            drA.Zipcode = txtZip.Text.Trim();
            drA.Notes = txtNotes.Text.Trim();
            drA.FromDate = txtFromDate.Text.Trim();
            drA.ToDate = txtToDate.Text.Trim();

            AddressBO aBO = new AddressBO();
            if (addressPK == 0)
                drA = aBO.AddRecord(drA);
            else
                drA = aBO.UpdateRecord(addressPK, drA);

            HasUnsavedChanges = false;
            return drA.AddressPK;
        }

        #endregion Populate & Save Methods
    }
}
